//
//  DateFormatter.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/23/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import Foundation


class FormatterDate {

    
    func findDifference (date: Date) -> String {
        let secondsAgo = Int(Date().timeIntervalSince(date))
        let minute = 60
        let hour = 60 * minute
        let day = 24 * hour
        let week = 7 * day
        
        let quotient: Int
        let unit: String
        
        if secondsAgo > week {
            return self.getDateString(date: date, at: "dd/MM/yyyy")
        } else {
            if secondsAgo < minute {
                quotient = secondsAgo
                unit = "сек."
            } else if secondsAgo < hour {
                quotient = secondsAgo / minute
                unit = "мин."
            } else if secondsAgo < day {
                quotient = secondsAgo / hour
                unit = "час."
            } else {
                quotient = secondsAgo / day
                unit = "дн."
            }
            
            return "\(quotient) \(unit)"
        }
    }
    
    func getLeftDays (timestamp: Int, maxDays: Int) -> String {
        return "60 дней осталось"
    }
    
    func getDateString (date: Date, at format: String) -> String {
        let dateFormatterGet = DateFormatter()
        dateFormatterGet.dateFormat = format
        return dateFormatterGet.string(from: date)
    }
    
    func getDate(inputDate: String) -> Date? {
         let dateFormatterGet = DateFormatter()
         dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
         let dateFormatterPrint = DateFormatter()
         dateFormatterPrint.dateFormat = "MMM dd yyyy HH:mm"
         let date: Date? = dateFormatterGet.date(from: inputDate)
         return date
    }
}


extension Date {
    var month: String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM"
        return dateFormatter.string(from: self)
    }
}
